capture log close
clear all
set more off
pause off
# delimit;

local output "";
local log "";
local data "";

/* 
Create Figure 2B
*/

use "`data'\collapsemsim_combine.dta", clear;


/* ******************************* */
/* **** DATA PREP AND RESHAPE **** */
/* ******************************* */

/* Create month-year and age variables (relative to December of that year) */
gen ser_dobym=ym(ser_dobyy, ser_dobmm);
gen age_month=ym(year,12)-ser_dobym;
replace age_month=9999 if year==9999;

/* Collapse by year cohort and age_month */
collapse (sum) earn10yr* earn55*, by(ser_dobym ser_dobyy ser_dobmm age_month);

/* Create fractions of cohorts instead of counts (for flow variable, use single event denominator to avoid double counting people) */
foreach set in earn10yr earn55 {;
foreach stub in `set'_lfexit_flow `set'_lfexit_one `set'_claim_phusold `set'_claim_phusany `set'_claim_mbaold `set'_claim_mbaany {;
	egen total_`stub'=total(`stub'), by(ser_dobym);
};
};
foreach set in earn10yr earn55 {;
foreach stub in `set'_lfexit_one `set'_claim_phusold `set'_claim_phusany `set'_claim_mbaold `set'_claim_mbaany {;	
	rename `stub' ct_`stub';
	gen `stub'=ct_`stub'/total_`stub' if ct_`stub'>10;
	replace `stub'=. if ct_`stub'<=10;
};
	rename `set'_lfexit_flow ct_`set'_lfexit_flow;
	gen `set'_lfexit_flow=ct_`set'_lfexit_flow/total_`set'_lfexit_one;
	replace `set'_lfexit_flow=. if ct_`set'_lfexit_flow<=10;
};
drop total_* ct_*;

/* Create <=1937 and >=1943 cohorts and collapse across YMoB to the YOB or broader cohort level */
drop if age_month<50*12;

tab ser_dobyy, m;
gen ser_dobcoh=1937 if ser_dobyy>=1935 & ser_dobyy<=1937;
replace ser_dobcoh=1943 if ser_dobyy>=1943 & ser_dobyy<=1945;
replace ser_dobcoh=9999 if ser_dobcoh==.;

collapse 
	earn10yr_lfexit_flow earn10yr_lfexit_one earn10yr_claim_phusold earn10yr_claim_phusany earn10yr_claim_mbaold earn10yr_claim_mbaany
	earn55_lfexit_flow earn55_lfexit_one earn55_claim_phusold earn55_claim_phusany earn55_claim_mbaold earn55_claim_mbaany
	, by(ser_dobcoh age_month);

/* Reshape for plot */
reshape wide 
	earn10yr_lfexit_flow earn10yr_lfexit_one earn10yr_claim_phusold earn10yr_claim_phusany earn10yr_claim_mbaold earn10yr_claim_mbaany
	earn55_lfexit_flow earn55_lfexit_one earn55_claim_phusold earn55_claim_phusany earn55_claim_mbaold earn55_claim_mbaany
, i(age_month) j(ser_dobcoh);


/* ***************************************************** */
/* **** PLOT RETIREMENT AND CLAIMING ACROSS COHORTS **** */
/* ***************************************************** */

local color_control "blue";
local color_treatment "red";

gen age=age_month/12;
drop if age_month==9999;

keep if age>=55 & age<69 ;

foreach set in earn10yr {;
foreach stub in lfexit_flow {;
twoway 	
	connected `set'_`stub'1937 age if age>=55 & age<69, lcolor(`color_control') lwidth(medthick) lpattern(solid) mcolor(`color_control') msymbol(sh) ||
	connected `set'_`stub'1943 age if age>=55 & age<69, lcolor(`color_treatment') lwidth(medthick) lpattern(solid) mcolor(`color_treatment') msymbol(o)
	xtitle("Age", size(medsmall)) xsc(r(55 69)) xlab(55(1)68)
	ytitle("Fraction of birth cohort", size(medsmall))
	legend(col(2) row(1) label(1 "Pre-Reform") label(2 "Post-Reform"))
	graphregion(color(white))
	bgcolor(white)
	ylabel(,nogrid)
	;
	graph export "`output'\\Figure_2B.png", replace;
};
};
